home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue63 / Docking / DockedControls8U.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-09-19  |  4.2 KB  |  144 lines

  1. unit DockedControls8U;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ComCtrls, ToolWin, StdCtrls, ImgList, ExtCtrls, ActnList, Menus;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     ImageList1: TImageList;
  12.     Label1: TLabel;
  13.     Panel1: TPanel;
  14.     Image1: TImage;
  15.     MainMenu1: TMainMenu;
  16.     ActionList1: TActionList;
  17.     actToggleToolbar: TAction;
  18.     Environment1: TMenuItem;
  19.     Toolbarvisible1: TMenuItem;
  20.     btnToggleFloat: TButton;
  21.     ToolBar1: TToolBar;
  22.     ToolButton6: TToolButton;
  23.     ToolButton7: TToolButton;
  24.     ToolButton8: TToolButton;
  25.     ToolButton9: TToolButton;
  26.     ToolButton10: TToolButton;
  27.     TopDockPanel: TPanel;
  28.     RightDockPanel: TPanel;
  29.     LeftDockPanel: TPanel;
  30.     BottomDockPanel: TPanel;
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure actToggleToolbarExecute(Sender: TObject);
  33.     procedure actToggleToolbarUpdate(Sender: TObject);
  34.     procedure btnToggleFloatClick(Sender: TObject);
  35.     procedure DockPanelGetSiteInfo(Sender: TObject; DockClient: TControl;
  36.       var InfluenceRect: TRect; MousePos: TPoint; var CanDock: Boolean);
  37.     procedure DockPanelDockOver(Sender: TObject;
  38.       Source: TDragDockObject; X, Y: Integer; State: TDragState;
  39.       var Accept: Boolean);
  40.     procedure DockPanelDockDrop(Sender: TObject;
  41.       Source: TDragDockObject; X, Y: Integer);
  42.   private
  43.     { Private declarations }
  44.   public
  45.     { Public declarations }
  46.   end;
  47.  
  48. var
  49.   Form1: TForm1;
  50.  
  51. implementation
  52.  
  53. {$R *.DFM}
  54.  
  55. procedure TForm1.FormCreate(Sender: TObject);
  56. begin
  57.   Mouse.DragImmediate := False;
  58.   Image1.ManualDock(Panel1);
  59.   ToolBar1.ManualDock(TopDockPanel);
  60. end;
  61.  
  62. procedure TForm1.actToggleToolbarExecute(Sender: TObject);
  63. begin
  64.   ToolBar1.Visible := not (Sender as TAction).Checked
  65. end;
  66.  
  67. procedure TForm1.actToggleToolbarUpdate(Sender: TObject);
  68. begin
  69.   (Sender as TAction).Checked := ToolBar1.Visible
  70. end;
  71.  
  72. procedure TForm1.btnToggleFloatClick(Sender: TObject);
  73. begin
  74.   if ToolBar1.Floating then
  75.     ToolBar1.ManualDock(TopDockPanel)
  76.   else
  77.     //ToolBar1.ManualDock(nil)
  78.     ToolBar1.ManualFloat(
  79.       Rect(Left, Top, Left + ToolBar1.UndockWidth, Top + ToolBar1.UndockHeight))
  80. end;
  81.  
  82. procedure TForm1.DockPanelGetSiteInfo(Sender: TObject; DockClient: TControl;
  83.   var InfluenceRect: TRect; MousePos: TPoint; var CanDock: Boolean);
  84. begin
  85.   //Allow for more margin of error
  86.   InflateRect(InfluenceRect, 10, 10);
  87.   //Only accept toolbars
  88.   CanDock := DockClient is TToolBar
  89. end;
  90.  
  91. procedure TForm1.DockPanelDockOver(Sender: TObject;
  92.   Source: TDragDockObject; X, Y: Integer; State: TDragState;
  93.   var Accept: Boolean);
  94. var
  95.   DockBar: TToolBar;
  96.   InflateSize: Integer;
  97.   ARect: TRect;
  98.   ClientTL: TPoint;
  99. begin
  100.   DockBar := Source.Control as TToolBar;
  101.   //Get current height if horizontal or width if vertical
  102.   if DockBar.Width > DockBar.Height then
  103.     InflateSize := DockBar.Height
  104.   else
  105.     InflateSize := DockBar.Width;
  106.   //Dock rect will be 0 width/height as per the panel dimensions
  107.   //To make it look realistic, increase rectangle
  108.   //to same width/height as toolbar
  109.   ARect := Source.DockRect;
  110.   case (Sender as TPanel).Align of
  111.     alTop: Inc(ARect.Bottom, InflateSize);
  112.     alLeft: Inc(ARect.Left, InflateSize);
  113.     alBottom: Dec(ARect.Top, InflateSize);
  114.     alRight: Dec(ARect.Right, InflateSize);
  115.   end;
  116.   //Two of the four aligned dock panels will not stretch
  117.   //along edge of form client area (because of the currently
  118.   //visible one's size). Make sure dock rectangles do
  119.   ClientTL := Point(0, 0);
  120.   ClientTL := ClientToScreen(ClientTL);
  121.   case (Sender as TPanel).Align of
  122.     alTop, alBottom:
  123.     begin //Make horizontal panels stretch across form's client width
  124.       ARect.Left := ClientTL.X;
  125.       ARect.Right := ClientTL.X + ClientWidth;
  126.     end;
  127.     alLeft, alRight:
  128.     begin //Make vertical panels stretch across form's client width
  129.       ARect.Top := ClientTL.Y;
  130.       ARect.Bottom := ClientTL.Y + ClientHeight;
  131.     end;
  132.   end;
  133.   Source.DockRect := ARect
  134. end;
  135.  
  136. procedure TForm1.DockPanelDockDrop(Sender: TObject;
  137.   Source: TDragDockObject; X, Y: Integer);
  138. begin
  139.   //Get toolbar to reset its dimensions for docking
  140.   (Source.Control as TToolBar).Align := (Sender as TPanel).Align;
  141. end;
  142.  
  143. end.
  144.